import {Component, Input, OnInit} from '@angular/core';
import {NgbActiveModal} from '@ng-bootstrap/ng-bootstrap';
import {Carousel} from '../../../model/Carousel';
import {CarouselService} from '../../../services/carousel.service';
import {ToastService} from '../../../services/toast.service';
import {NewsService} from '../../../services/news.service';
import {ConfigService} from '../../../services/config.service';

@Component({
  selector: 'app-carouseledit',
  templateUrl: './carouseledit.component.html',
  styleUrls: ['./carouseledit.component.css']
})
export class CarouseleditComponent implements OnInit {
  @Input() id;
  c:Carousel;
  constructor(public activeModal: NgbActiveModal
  ,private carouselservice:CarouselService
  ,private toastservice:ToastService
  ,private newsservice:NewsService
  ,public config:ConfigService) {
    this.c=new Carousel();

  }

  ngOnInit(): void {
    console.log('接收到的参数是'+this.id);
    if(this.id!='add'){
      this.getCarousel();
    }

  }

  getCarousel(){
    this.carouselservice.getCarousel(this.id)
      .then((data:any)=>{
        if(data.msg=='ok'){
          this.c=new Carousel();
          this.c=data.obj;
        }
        else {
          this.toastservice.showError('获取数据异常请重试');
        }

      })
  }

  save(){
    this.carouselservice.saveCarousel(this.c)
      .then((data:any)=>{
        if(data.msg=='ok'){
        this.goBack('saved');
        }
        else {
          this.toastservice.showError('保存异常请重试');
        }
      })
  }

  goBack(state:string){
    this.activeModal.close(state);
  }


  onFileChange(e:any){
    let touplocadfile=e.target.files[0];
    this.newsservice.uploadPic(touplocadfile)
      .then((data:any)=>{
        if(data.msg=='ok'){
          this.c.pic=data.filename;
        }
        else {
          this.toastservice.showError('上传失败，请重试');
        }
      })

  }
}
